#include "file_manager.h"

// 获取文件信息
int get_file_info(const char *path, SimpleFileInfo *info) {
    struct stat stat_buf;
    
    if (stat(path, &stat_buf) == -1) {
        return -1;
    }
    
    // 提取文件名
    const char *filename = strrchr(path, '/');
    if (filename != NULL) {
        strcpy(info->name, filename + 1);
    } else {
        strcpy(info->name, path);
    }
    
    strcpy(info->path, path);
    info->size = stat_buf.st_size;
    info->modified_time = stat_buf.st_mtime;
    info->permissions = stat_buf.st_mode;
    info->is_directory = S_ISDIR(stat_buf.st_mode);
    
    return 0;
}

// 打印文件信息
void print_file_info(const SimpleFileInfo *info) {
    char time_str[64];
    struct tm *time_info = localtime(&info->modified_time);
    strftime(time_str, sizeof(time_str), "%Y-%m-%d %H:%M:%S", time_info);
    
    printf("文件信息:\n");
    printf("--------------------------------\n");
    printf("名称: %s\n", info->name);
    printf("路径: %s\n", info->path);
    printf("类型: %s\n", info->is_directory ? "目录" : "文件");
    printf("大小: %ld 字节\n", info->size);
    printf("修改时间: %s\n", time_str);
    printf("权限: %o\n", info->permissions & 0777);
    // 检查文件是否被锁定
    if (file_is_locked(info->path)) {
        printf("状态: 已锁定\n");
    } else {
        printf("状态: 未锁定\n");
    }
    printf("--------------------------------\n");
}